package com.base.mail;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.bean.DepartMentBean;
import com.base.mail.adapter.AJDepartAdapter;
import com.base.mail.databinding.ActivityAjdepartBinding;
import com.base.mail.presenter.AjDepartPresenter;
import com.base.mail.tree.Node;
import com.base.mail.view.AjDepartView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Created by weikailiang on 2019/11/18.
 */
@Route(path = "/mail/depart")
public class AjDepartActivity extends CBaseBarMvpActivity<AjDepartPresenter> implements AjDepartView{
    private ActivityAjdepartBinding mBinding;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_ajdepart;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityAjdepartBinding) mRootBinding;
        mPresenter = new AjDepartPresenter(this,this);
        setBackNavigation();
        setBarTitle("选择部门");

    }

    @Override
    protected void start() {
        mPresenter.start();
        mPresenter.getParenter();
        mBinding.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (AJDepartAdapter.mMapPart.size()==0){showMes("请选择部门");return;}
                List<DepartMentBean> nodes = new ArrayList<>();
                DepartMentBean bean = null;
                for (Map.Entry<String, Node> node : AJDepartAdapter.mMapPart.entrySet()){
                    bean  = new DepartMentBean();
                    bean.setId(node.getValue().getId().toString());
                    bean.setName(node.getValue().getName());
                    nodes.add(bean);
                }
                Intent in = new Intent();
                in.putExtra("depart",new Gson().toJson(nodes));
                setResult(RESULT_OK,in);
                finish();
            }
        });
    }

    @Override
    public RecyclerView getRecycler() {
        return mBinding.rv;
    }

    @Override
    public void returnPartner(String departName) {
        mBinding.name.setText(departName);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (AJDepartAdapter.mMapPart.size()>0){
            AJDepartAdapter.mMapPart.clear();
        }
        mPresenter.release();
    }
}
